﻿function defaultx(obj, name) {
	if (Ext.isEmpty(obj, true))
		return {};
	else if (typeof(obj) != 'object') {
		var ret = {};
		ret[name] = obj;
		return ret;
	} else
		return obj;
}

function ajax(obj) {
	Ext.Ajax.request({
				url : obj.url,
				method : obj.method || 'POST',
				params : obj.params,
				success : function(response) {
					var rsp = Ext.util.JSON.decode(response.responseText);
					if (obj.handler)
						obj.handler(rsp);
					else if (rsp.success) {
						delete rsp.success;
						if (obj.success) obj.success(rsp);
					} else {
						delete rsp.success;
						if (obj.failure)
							obj.failure(rsp);
						else if (!obj.nomsg) alerterr(rsp.msg);
					}
				},
				failure : function(response) {
					if (sys.debug)
						alerterr(response.responseText);
					else
						alert('网络错误，请稍后重试！');
				}
			});
}
function ajaxform(obj) {
	obj.form.getForm().submit({
				waitTitle : obj.waitTitle || '提示',
				waitMsg : obj.nowait ? null : obj.waitmsg || '正在提交，请稍候...',
				method : obj.method || 'POST',
				params : obj.params,
				url : obj.url,
				success : function(form, action) {
					if (obj.handler)
						obj.handler(action.result);
					else {
						delete action.result.success;
						if (obj.success) obj.success(action.result);
					}
				},
				failure : function(form, action) {
					if (action.failureType == Ext.form.Action.SERVER_INVALID) {
						if (obj.handler)
							obj.handler(action.result);
						else {
							delete action.result.success;
							if (obj.failure)
								obj.failure(action.result);
							else if (!obj.nomsg) alerterr(action.result.msg);
						}
					} else if (sys.debug)
						alerterr(action.response.responseText);
					else
						alert('网络错误，请稍候重试！');
				}
			});
}

function alertx(msg) {
	Ext.Msg.show({
				title : '提示',
				msg : msg,
				icon : Ext.Msg.INFO,
				buttons : Ext.Msg.OK
			});
}
function alerterr(msg) {
	Ext.Msg.show({
				title : '错误',
				msg : msg,
				icon : Ext.Msg.ERROR,
				buttons : Ext.Msg.OK
			});
}
function confirmx(msg, handler) {
	Ext.Msg.confirm('提示', msg, function(btn) {
				if (btn == 'yes') handler();
			});
}

function HTMLEncode(str) {
	var s = "";
	if (Ext.isEmpty(str.length)) return "";
	s = str.replace(/&/g, "&amp;");
	s = s.replace(/</g, "&lt;");
	s = s.replace(/>/g, "&gt;");
	s = s.replace(/ /g, "&nbsp;");
	s = s.replace(/\’/g, "&#39;");
	s = s.replace(/\"/g, "&quot;");
	return s;
}
function string2time(time) {
	if (Ext.isEmpty(time)) return undefined;
	time = time.toString().replace(/\d+(?=-[^-]+$)/g, function(a) {
				return parseInt(a, 10) - 1;
			}).match(/\d+/g);
	if (time.length > 3)
		return new Date(time[0], time[1], time[2], time[3], time[4], time[5]);
	else
		return new Date(time[0], time[1], time[2]);
};
function isNoBlank(arr) {
	if (!Ext.isArray(arr)) arr = [arr];
	for (var i = 0; i < arr.length; ++i)
		if (Ext.util.Format.trim(Ext.util.Format.stripTags(arr[i].getValue())) == '') return false;
	return true;
}

// 数字pid转换字母pid
function int2char(value) {
	return String.fromCharCode(parseInt(value.toString()) + 65);
}

function openWindowWithPost(url, keys, values) {
	var oForm = document.createElement('form');
	oForm.id = Ext.id();
	oForm.method = 'post';
	oForm.action = url;
	oForm.target = 'test123';
	if (keys && values && (keys.length == values.length)) {
		for (var i = 0; i < keys.length; i++) {
			var oInput = document.createElement('input');
			oInput.type = 'text';
			oInput.name = keys[i];
			oInput.value = values[i];
			oForm.appendChild(oInput);
		}
	}
	document.body.appendChild(oForm);
	oForm.submit();
}
function openWindow(url) {
	openWindowWithPost(url, [], []);
}

function getFuncName(func) {
	return func.toString().match(/^function\s+([^\(\s]+)/)[1];
}

String.prototype.gblen = function() {
	var len = 0;
	for (var i = 0; i < this.length; i++) {
		if (this.charCodeAt(i) > 127 || this.charCodeAt(i) == 94) {
			len += 2;
		} else {
			len++;
		}
	}
	return len;
}
String.prototype.gbtrim = function(len, s) {
	var str = '';
	var sp = s || '...';
	var len2 = 0;
	for (var i = 0; i < this.length; i++) {
		if (this.charCodeAt(i) > 127 || this.charCodeAt(i) == 94) {
			len2 += 2;
		} else {
			len2++;
		}
	}
	if (len2 <= len) {
		return this;
	}
	len2 = 0;
	len = (len > sp.length) ? len - sp.length : len;
	for (var i = 0; i < this.length; i++) {
		if (this.charCodeAt(i) > 127 || this.charCodeAt(i) == 94) {
			len2 += 2;
		} else {
			len2++;
		}
		if (len2 > len) {
			str += sp;
			break;
		}
		str += this.charAt(i);
	}
	return str;
}

Ext.data.Store.prototype.reloadParams = function(params) {
	this.reload({
				params : Ext.applyIf(params || {}, this.lastOptions.params)
			});
}
Ext.PagingToolbar.prototype.doLoad = function(start) {
	var o = {}, pn = this.getParams();
	o[pn.start] = start;
	o[pn.limit] = this.pageSize;
	if (this.fireEvent('beforechange', this, o) !== false) {
		this.store.reloadParams(o);
	}
}
